<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Meiti_model extends CI_Model {
	
	function __construct(){
        parent::__construct();
    }
    
    function get_list_index($data = array()){
    	$mkey = "meiti_list_index";
    	//$this->mycache->delete($mkey);
    	$ret = $this->mycache->get_array($mkey);
    	if($ret != false){
    		return $ret;
    	}
    	//封装查询内容
    	$_select = "*";
    
    	//封装条件
    	$_where = " where 1=1 ";
    	if(isset($data['where']['type']) && $data['where']['type']){
    		$_where .= " and type = '{$data['where']['type']}'";
    	}
    	if(isset($data['where']['title']) && $data['where']['title'] != ''){
    		$_where .= " and title like '%".$data['where']['title']."%'";
    	}
    	$_order = " order by id desc ";
    	$_limit = " limit ".$data['page_start'].",".$data['page_size'];
    	//封装sql
    	$sql_result = "select $_select from zjy_meiti ";
    	$sql_result .= $_where . $_order . $_limit;
    
    	$query = $this->db->query($sql_result);
    	$list = $query->result_array();
    	$this->mycache->set_array($mkey,$list, 3*60*60);
    	return $list;
    }
    
    function get_list($data = array()){
    	//封装查询内容
		$_select = "*";
		
		//封装条件
		$_where = " where 1=1 ";
		if(isset($data['where']['type']) && $data['where']['type']){
			$_where .= " and type = '{$data['where']['type']}'";
		}
		if(isset($data['where']['title']) && $data['where']['title'] != ''){
			$_where .= " and title like '%".$data['where']['title']."%'";
		}
		$_order = " order by id desc ";
		$_limit = " limit ".$data['page_start'].",".$data['page_size'];
		//封装sql
		$sql_result = "select $_select from zjy_meiti ";
		$sql_result .= $_where . $_order . $_limit;
		
		$query = $this->db->query($sql_result);
		$ret['list'] = $query->result_array();
		
		$sql_count = "select count(*) as count from zjy_meiti ";
		$sql_count .= $_where;
		$query = $this->db->query($sql_count);
		$ret['count'] = $query->row()->count;
		return $ret;
    }
    
    function get_one($id){
    	$this->db->where('id', $id);
		$query = $this->db->get('zjy_meiti');
		return $query->row_array();
    }
    
    function update($data=array(),$where=array()){
    	$this->mycache->delete('meiti_list_index');
    	$this->db->where($where);
		$this->db->update('zjy_meiti', $data);
		$res = $this->db->affected_rows();
		return $res;
    }
    
    function add($data=array()){
    	$this->mycache->delete('meiti_list_index');
    	$this->db->insert('zjy_meiti', $data);
		return $this->db->insert_id();
    }
    
}